package com.crm.jdy.service.usermanager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.crm.jdy.common.dto.RoleInfoDto;
import com.crm.jdy.common.dto.UserInfoDto;
import com.crm.jdy.common.myexception.MyException;
import com.crm.jdy.common.pojo.ClientChargeInfo;
import com.crm.jdy.common.pojo.UserInfo;
import com.crm.jdy.common.mapper.UserInfoMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**   
 * @Description:(用户管理服务实现)
 *
 * @version: V1.0
 * @author: kong
 * 
 */
@Service(version = "${service.version}")
@Component
public class UserInfoServiceImpl  extends ServiceImpl<UserInfoMapper,UserInfo> implements UserInfoService  {

    @Autowired
    UserInfoMapper userInfoDao;

    @Override
    public PageInfo<UserInfo> seleteUserInfo(UserInfoDto callAplanInfoDto) {
        PageHelper.startPage( callAplanInfoDto.getCurrentPage(),callAplanInfoDto.getPageSize());
        List<UserInfo> bookList = userInfoDao.seleteUserInfo(callAplanInfoDto);
        PageInfo<UserInfo> pageInfo = new PageInfo<UserInfo>(bookList);
        return pageInfo;
    }

    @Override
    public boolean updateUserInfo(UserInfo callAplanInfoDto) {
        return userInfoDao.updateById(callAplanInfoDto) > 0;
    }

    @Override
    public boolean addUserInfo(UserInfo callAplanInfo)  {
        LambdaQueryWrapper<UserInfo> lambda = new QueryWrapper<UserInfo>().lambda().last("limit 1")
                .eq(UserInfo::getUserName, callAplanInfo.getUserName());
        UserInfo clientInfo = userInfoDao.selectOne(lambda);
        System.out.println("----------------");
        System.out.println(clientInfo);
        if( clientInfo !=null && !ObjectUtils.isEmpty(clientInfo)){
            return false;
        }else{
            return userInfoDao.insert(callAplanInfo) > 0;
        }
    }

    @Override
    public boolean removeUserInfo(String aplanId) {
        return userInfoDao.deleteById(aplanId) > 0;
    }

//    根据用户名获取角色信息并获取用户权限信息
    @Override
    public RoleInfoDto getRoleInfo(String username) {
        return userInfoDao.getRoleInfo(username);
    }

//    根据用户名获取密码
    @Override
    public String getPassword(String username) {
        return userInfoDao.getPassword(username);
    }

    @Override
    public boolean seleteVerify(String id) {
        UserInfo userInfo = userInfoDao.selectById(id);
        if(ObjectUtils.isEmpty(userInfo)  || null == userInfo ){
              return false;
        }
        return  true;
    }

    @Override
    public UserInfo getUserByName(String name) {
        LambdaQueryWrapper<UserInfo> lambda = new QueryWrapper<UserInfo>().lambda().last("limit 1")
                .eq(UserInfo::getUserName, name);
        return userInfoDao.selectOne(lambda);
    }

    @Override
    public UserInfoDto getUserDetails(String name) {
        return userInfoDao.getUserDetails(name);
    }

    @Override
    public Boolean updateInfo(UserInfo userInfo) {
        UpdateWrapper<UserInfo> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.eq("user_per_id", userInfo.getUserPerId() );
        return userInfoDao.update(userInfo,userUpdateWrapper)>0;
    }


}